import React, { useState, useEffect, useCallback } from 'react'
import { useHistory } from 'react-router-dom'
import { putAccElseAddElse } from '../../../../financialApi'
import { getSysUserSelected } from '../../../../../../Api/listUrl'

import moment from 'moment'
import { Button, Form, Input, InputNumber, message, Select } from 'antd'

const { TextArea } = Input;
const { Option } = Select;


/**
 * @description: 财务--出差申请表
 * @author: xiaoxujs
 * @update: xiaoxujs（2021-07-08）
 */

const ElseCost = ({ formData }) => {
    const history = useHistory()
    const [form] = Form.useForm()
    const { setFieldsValue } = form
    const [buttonLoading, setButtonLoading] = useState(false)
    const [userList, setUserList] = useState([])

    const userFun = useCallback(
        () => {
            ; (async () => {
                const { code, msg, data } = await getSysUserSelected()
                if (code === '20000') {
                    setUserList(data)
                } else {
                    message.error(msg)
                }
            })()
        },
        [],
    )
    useEffect(() => {
        userFun()
        setFieldsValue(formData)
    }, [formData, setFieldsValue, userFun])
    const onFinish = (values) => {
        values.startTime = moment(values.startTime).format('YYYY-MM-DD')
        values.endTime = moment(values.endTime).format('YYYY-MM-DD')
            ; (async () => {
                setButtonLoading(true)
                const { code, msg } = await putAccElseAddElse(values)
                if (code === '20000') {
                    message.success('提交成功')
                    setButtonLoading(false)
                    history.go('-1')
                } else {
                    setButtonLoading(false)
                    message.error(msg)
                }
            })()
    };
    return <Form
        labelCol={{ span: 3 }}
        wrapperCol={{ span: 16 }}
        onFinish={onFinish}
    >
        <Form.Item
            label="申请人"
            name="applicant"
            rules={[{ required: true, message: '请选择申请人!' }]}
        >
            <Select>
                {
                    userList.map(item => <Option value={item.id} key={item.id}>{item.name}</Option>)
                }
            </Select>
        </Form.Item>
        <Form.Item
            label="部门主管"
            name="director"
            rules={[{ required: true, message: '请选择部门主管!' }]}
        >
            <Select>
                {
                    userList.map(item => <Option value={item.id} key={item.id}>{item.name}</Option>)
                }
            </Select>
        </Form.Item>
        <Form.Item
            label="借款事由"
            name="matter"
            rules={[{ required: true, message: '请输入借款事由!' }]}
        >
            <TextArea />
        </Form.Item>

        <Form.Item
            label="申请金额（元）"
            name="money"
            rules={[{ required: true, message: '请输入申请金额!' }]}
        >
            <InputNumber />
        </Form.Item>
        <Form.Item
            label="备注"
            name="remark"
        >
            <TextArea />
        </Form.Item>

        <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
            <Button type="primary" htmlType="submit" loading={buttonLoading}>
                提交申请
            </Button>
        </Form.Item>
    </Form>
}

export default ElseCost